package Corpus;
//TODO do that the iterator when he go over the file in the first this map him in file and not load the heap all the File of them in the secend iteretion
import java.io.*;
import java.util.*;

import DataObjects.*;
import Main.*;
import XMLCreator.XMLSpliter2;

public class WikiCorpus implements Corpus {

	private class WikiCorpusIterator implements Iterator<Pair<Article, String>>{

		private BufferedReader bufferedReader;
		private String nextLine;
		
		public WikiCorpusIterator() throws IOException{
			bufferedReader=new BufferedReader(new FileReader(new File(allArticlesFileName)));
			System.out.println("WikiCorpus.WikiCorpusIterator.contractor "+allArticlesFileName);//TODO
			nextLine=bufferedReader.readLine();
		}

		public boolean hasNext() {
			return nextLine!=null;
		}

		private Article makeArticle(String path){
			String title="";
			Vector<String> parts=String2.split(path,"\\");
			String name=parts.get(parts.size()-1);
			LinkedList<Pair<String,Object>> metadata=new LinkedList<Pair<String,Object>>();
			File textFile=new File(path);
			File metadataFile=new File(nameDirOutMetadata+name);
			try {
				
				FileReader reader=new FileReader(metadataFile);
				BufferedReader buf=new BufferedReader(reader);

				String line=buf.readLine();
				while(line!=null && !line.trim().equals("")){
					String[] splited=line.split(" == ");
					if (splited[0].equals("title")){
						title=splited[1];
					}
					else if (splited[1]==null || splited[1].equals("null")){
						metadata.add(new Pair<String, Object>(splited[0],"Unknown"));		
					}
					else if (splited[0].equals("cat")){
						if (splited[1].equals("[]")){
							metadata.add(new Pair<String, Object>(splited[0],"Unknown"));		
						}
						else{
							Vector<String> cats=String2.split(splited[1].substring(1, splited[1].length()-1),", ");
							for(String cat : cats){
								metadata.add(new Pair<String, Object>(splited[0], cat));
							}
						}
					}
					else{
						metadata.add(new Pair<String, Object>(splited[0], splited[1]));			
					}
					line=buf.readLine();
				}

				buf.close();
				reader.close();

				String text="";
				reader=new FileReader(textFile);
				buf=new BufferedReader(reader);
				line=buf.readLine();
				while(line!=null){
					text+=line+"\n";
					line=buf.readLine();
				}				
				if (text.trim().equals(""))
				System.out.println("text id: "+Integer.parseInt(String2.split(name, ".").get(0)));
				buf.close();
				reader.close();
				return new Article(Integer.parseInt(String2.split(name, ".").get(0)) ,title, text, metadata);
			} catch (FileNotFoundException e) {
				e.printStackTrace();//TODO do samthing or dont do samthing
			} catch (IOException e) {
				e.printStackTrace();//TODO do samthing or dont do samthing
			}

			return null;			
		}

		public Pair<Article, String> next() {
			try {
				String curntLine=nextLine;
				nextLine=bufferedReader.readLine();
				String path=nameDirOutText+curntLine;
				return new Pair<Article, String>(makeArticle(path),path);
			} catch (IOException e) {
				System.out.println("WikiCorpus.WikiCorpusIterator.next IOException");//TODO
				System.out.println(e);//TODO
				return null;
			}
		}

		public void remove() {
		}
	}

	private String fileInStart;
	private String fileInEnd;
	private int FirstIndex;
	private int lastIndex;
	private String nameDirOutText;
	private String nameDirOutHtml;
	private String nameDirOutMetadata;
	private String allArticlesFileName;

	public WikiCorpus(String fileInStart,String fileInEnd,int FirstIndex,int lastIndex,String nameDirOutTest,String nameDirOutMetadata,String allArticlesFileName,String nameDirOutHtml){
		this.fileInStart=fileInStart;
		this.fileInEnd=fileInEnd;
		this.FirstIndex=FirstIndex;
		this.lastIndex=lastIndex;
		this.nameDirOutText=nameDirOutTest;
		this.nameDirOutHtml=nameDirOutHtml;
		this.nameDirOutMetadata=nameDirOutMetadata;
		this.allArticlesFileName=allArticlesFileName;
	}

	public Vector<Threesome<String,String,MetadataType>> getAllMetadataNames() {
		Vector<Threesome<String,String,MetadataType>> ans=new Vector<Threesome<String,String,MetadataType>>();

		ans.add(new Threesome<String,String,MetadataType>("author","Author",MetadataType.string));
		ans.add(new Threesome<String,String,MetadataType>("last_modified","Date",MetadataType.date));
		ans.add(new Threesome<String,String,MetadataType>("cat","Category",MetadataType.array));//TODO

		return ans;
	}

	public String getArticlesPath() {
		return nameDirOutText;
	}

	public void init() {
		Vector<Pair<String,String>> allPair=new Vector<Pair<String,String>>();
		
		File articles=new File(allArticlesFileName);
		try {
			FileWriter articlesWriter=new FileWriter(articles);
			
			
			allPair.add(new Pair<String, String>("author", "username"));
			allPair.add(new Pair<String, String>("last_modified", "timestamp"));
			allPair.add(new Pair<String, String>("title", "title"));

			for(int i=FirstIndex;i<=lastIndex;i++){
				System.out.println("wikiCorpus.init : "+fileInStart+i+fileInEnd);//TODO
				XMLSpliter2.makeSplit(fileInStart+i+fileInEnd,nameDirOutText,nameDirOutMetadata,nameDirOutHtml,allPair,articlesWriter);
			}
			articlesWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getAdderssPrefix(){
		return "wiki";
	}

	public Iterator<Pair<Article, String>> getIteratorForTheArticles() {
		try {
			return new WikiCorpusIterator();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
